package de.chessgame;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemReed;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import de.chessgame.block.BlockChessBoard;
import de.chessgame.block.BlockChessBoard;
import de.chessgame.handler.ChessGameGuiHandler;
import de.chessgame.network.ChessMessage;
import de.chessgame.network.ChessMessageHandler;
import de.chessgame.proxy.CommonProxy;
import de.chessgame.tileentity.TileEntityChessBoard;

@Mod(modid = ChessGameMod.MODID, name = ChessGameMod.NAME, version = ChessGameMod.VERSION)
public class ChessGameMod {
	public static final String MODID = "chessgame";
	public static final String VERSION = "1.1.1";
	public static final String NAME = "chessgame";

	@Instance("chessgame")
	public static ChessGameMod instance;

	
	public static SimpleNetworkWrapper simpleNetworkWrapper;

	// chess pieces:
	public static Item itemKingWhite;
	public static Item itemQueenWhite;
	public static Item itemRookWhite;
	public static Item itemKnightWhite;
	public static Item itemBishopWhite;
	public static Item itemPawnWhite;

	public static Item itemKingBlack;
	public static Item itemQueenBlack;
	public static Item itemRookBlack;
	public static Item itemKnightBlack;
	public static Item itemBishopBlack;
	public static Item itemPawnBlack;

	public static ItemReed itemChessBoard;
	public static Block blockChessBoard;

	public static CreativeTabs tabChessGame = new CreativeTabs("chessgame") {

		@Override
		public Item getTabIconItem() {
			return itemChessBoard;
		}

	};

	@SidedProxy(clientSide = "de.chessgame.proxy.ClientProxy", serverSide = "de.chessgame.proxy.CommonProxy")
	public static CommonProxy proxy;

	@EventHandler
	public void preInit(FMLInitializationEvent event) {
		simpleNetworkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel(MODID);
		simpleNetworkWrapper.registerMessage(ChessMessageHandler.class,
				ChessMessage.class, 0, Side.SERVER);

		registerBlocks();
		registerItems();

		registerRecipes();

		proxy.registerRenderers();
		proxy.registerTileEntitySpecialRenderer();
	}

	@EventHandler
	public void init(FMLInitializationEvent event) {

		GameRegistry.registerTileEntity(TileEntityChessBoard.class,
				"blockchessboard");
		NetworkRegistry.INSTANCE.registerGuiHandler(this,
				new ChessGameGuiHandler());

	}

	private void registerRecipes() {
		ItemStack woolWhite = new ItemStack(Blocks.wool);
		ItemStack woolBlack = new ItemStack(Blocks.wool);
		woolWhite.setItemDamage(0);
		woolBlack.setItemDamage(15);
		GameRegistry.addShapelessRecipe(new ItemStack(itemChessBoard), woolWhite,
				woolBlack);
	}

	private void registerBlocks() {
		String blockChessBoardName = "blockchessboard";
		blockChessBoard = new BlockChessBoard()
				.setBlockName(blockChessBoardName).setBlockTextureName(MODID + ":" + blockChessBoardName);
		GameRegistry.registerBlock(blockChessBoard, blockChessBoardName);
	}

	private void registerItems() {
		// chessTable:
		String itemChessBoardName = "itemchessboard";

		itemChessBoard = new ItemReed(blockChessBoard);
		itemChessBoard.setUnlocalizedName(itemChessBoardName);
		itemChessBoard.setCreativeTab(tabChessGame);
		itemChessBoard.setTextureName(MODID + ":" + itemChessBoardName);
		GameRegistry.registerItem(itemChessBoard, itemChessBoardName, MODID);

		itemKingWhite = createAndRegisterPiece("kingwhite");
		itemQueenWhite = createAndRegisterPiece("queenwhite");
		itemRookWhite = createAndRegisterPiece("rookwhite");
		itemKnightWhite = createAndRegisterPiece("knightwhite");
		itemBishopWhite = createAndRegisterPiece("bishopwhite");
		itemPawnWhite = createAndRegisterPiece("pawnwhite");

		itemKingBlack = createAndRegisterPiece("kingblack");
		itemQueenBlack = createAndRegisterPiece("queenblack");
		itemRookBlack = createAndRegisterPiece("rookblack");
		itemKnightBlack = createAndRegisterPiece("knightblack");
		itemBishopBlack = createAndRegisterPiece("bishopblack");
		itemPawnBlack = createAndRegisterPiece("pawnblack");

	}

	private Item createAndRegisterPiece(String name) {
		Item item = new Item().setUnlocalizedName(name)
				.setTextureName(MODID + ":" + name).setMaxStackSize(1);
		GameRegistry.registerItem(item, name, MODID);
		return item;
	}
}
